﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPF_QC.QA_ViewModel;
using WPF_QC.ServiceReference2;
//using Microsoft.Office.Interop.Excel;    //NEED TO RESOLVE ASSEMBLY VERSION CONFLICTS ON MY LAPTOP



namespace WPF_QC.QA_View
{
    /// <summary>
    /// Interaction logic for MeasurementReports.xaml
    /// </summary>
    public partial class MeasurementReports : UserControl
    {
        MeasurementReportsViewModel viewModel = new MeasurementReportsViewModel();
        private ServiceReference2.Service1Client servRef2 = new ServiceReference2.Service1Client();
        private static DateTime startDate;
        private static DateTime endDate;
        private static string timeFrame;
        private static string type;


        public MeasurementReports()
        {
            InitializeComponent();
            this.Loaded += (s, e) => { this.DataContext = this.viewModel; };
            startDatePicker.DisplayDateStart = DateTime.Today.AddMonths(-3);
            startDatePicker.DisplayDateEnd = DateTime.Today.AddDays(-1);
            endDatePicker.DisplayDateStart = DateTime.Today.AddMonths(-3);
            endDatePicker.DisplayDateEnd = DateTime.Today;

        }

        private void timeFrameComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (timeFrameComboBox.SelectedItem != null)
            {
                if (timeFrameComboBox.SelectedItem == timeFrmVariable)
                {
                    selectTimeFrameWrapPanel.Visibility = Visibility.Visible;
                    startDatePicker.SelectedDate = null;
                    endDatePicker.SelectedDate = null;
                }
                else
                    if (selectTimeFrameWrapPanel != null)
                        selectTimeFrameWrapPanel.Visibility = Visibility.Hidden;
            }
        }

        private void viewReportButton_Click(object sender, RoutedEventArgs e)
        {
            timeFrame = Convert.ToString(this.timeFrameComboBox.Text);
            type = Convert.ToString(this.reportTypeComboBox.Text);
            reportTypeTextBlock.Text = type;

            if (timeFrame != "Variable Time Period")
            {
                switch (timeFrame)
                {
                    case "Previous Week":
                        startDate = DateTime.Today.AddDays(-7);
                        endDate = DateTime.Today;
                        reportDateTextBlock.Text = startDate.ToShortDateString() + " - " + endDate.ToShortDateString();
                        break;

                    case "Previous Month":
                        startDate = DateTime.Today.AddMonths(-1);
                        endDate = DateTime.Today;
                        reportDateTextBlock.Text = startDate.ToShortDateString() + " - " + endDate.ToShortDateString();
                        break;

                    case "Last 3 Months":
                        startDate = DateTime.Today.AddMonths(-3);
                        endDate = DateTime.Today;
                        reportDateTextBlock.Text = startDate.ToShortDateString() + " - " + endDate.ToShortDateString();
                        break;
                }
            }
            else
            {
                startDate = Convert.ToDateTime(startDatePicker.SelectedDate);
                endDate = Convert.ToDateTime(endDatePicker.SelectedDate);
                reportDateTextBlock.Text = startDate.ToShortDateString() + " - " + endDate.ToShortDateString();
            }
            reportDatagrid.ItemsSource = servRef2.GetReportData(startDate, endDate, type).ToList<Report>();
            reportCanvas.Visibility = Visibility.Visible;
          

        }

        private void printReportButton_Click(object sender, RoutedEventArgs e)
        {
            /*  NEED TO RESOLVE ASSEMBLY VERSION CONFLICT ERROR ON MY LAPTOP BEFORE USING THIS
            Microsoft.Office.Interop.Excel.Application exapp = new Microsoft.Office.Interop.Excel.Application();
            Workbook wb = exapp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
            Worksheet ws = (Worksheet)wb.Worksheets[1];
            Range aRange = ws.get_Range("A1");
            aRange.Value = "Test";
            int iRow;
            int iCol;
            iRow = 1;
            iCol = 1;
            foreach (Report rItem in rptData)
            {
                iCol = 1;
                ((Range)ws.Cells[iRow, iCol]).Value = rItem.MaterialName;
                iCol += 1;
                ((Range)ws.Cells[iRow, iCol]).Value = rItem.OnTime;
                iCol += 1;
                ((Range)ws.Cells[iRow, iCol]).Value = rItem.Pass_Fail;
                iCol += 1;
                ((Range)ws.Cells[iRow, iCol]).Value = rItem.Delayed;
                iCol += 1;
                ((Range)ws.Cells[iRow, iCol]).Value = rItem.DateTaken;

                iRow += 1;
            }
            exapp.Visible = true;
            */
        }

    }
}
